import { http } from '../utils/http'
import { LoginInfo, LoginResult } from '@/types/modules/login'
import router from '@/router'
import { useLoginAccountStoreWithOut } from '@/store/modules/login'

import axios from "axios";

const service = axios.create({
  baseURL: 'https://172.25.13.69:5234',
  timeout: 216000,
  withCredentials: false,
  headers: {
    'Content-Type': 'application/json;charset=UTF-8'
  }
});

const loginAccountStore = useLoginAccountStoreWithOut()

export const onLogin = async (data: LoginInfo) => {
  let res = await service.post('/api/account', data)

  if (res.data.success === true) {
    loginAccountStore.initLoginAccount();

    loginAccountStore.setUsername(res.data.name)
    loginAccountStore.setToken(res.data.accessToken)       
  }
  
  return res;
}

export const onRegister = async (data: { account: string; password: string }) => {
  return http.request('post', '/api/account/Register', {
    data
  })
}